<?php
/**
 * 销售物料模型层
 *
 * @author kamisama.xia@gmail.com 
 * @version 0.1
 */

class material_mdl_sales_material extends dbeav_model{

    var $defaultOrder = array('sm_id',' DESC');

    static public $__shopLists = array();

    /**
     * 销售物料类型字段格式化
     * @param string $row 物料类型字段
     * @return string
     */
    function modifier_sales_material_type($row){
        if($row == '1'){
            return '普通';
        }elseif($row == '2'){
            return '促销';
        }elseif($row == '3'){
            return '赠品';
        }else{
            return '-';
        }
    }

    /**
     * 物料是否可见字段格式化
     * @param string $row 物料是否可见字段
     * @return string
     */
    function modifier_shop_id($row){

        if(empty(self::$__shopLists)){
            self::$__shopLists['_ALL_'] = '全部店铺';
            $shopObj = app::get('ome')->model('shop');
            $tmp_shops = $shopObj->getList('shop_id,name',array(),0,-1);
            foreach($tmp_shops as $k => $shop){
                self::$__shopLists[$shop['shop_id']] = $shop['name'];
            }
        }
        
        return isset(self::$__shopLists[$row]) ? self::$__shopLists[$row] : $row;
    }

    /**
     * 基础物料列表项扩展字段
     */
    function extra_cols(){
        return array(
            'column_retail_price' => array('label'=>'售价','width'=>'60','func_suffix'=>'retail_price'),
            'column_unit' => array('label'=>'包装单位','width'=>'60','func_suffix'=>'unit'),
        );
    }

    /**
     * 售价扩展字段格式化
     */
    function extra_retail_price($rows){
        return kernel::single('material_extracolumn_salesmaterial_retailprice')->process($rows);
    }

    /**
     * 包装单位扩展字段格式化
     */
    function extra_unit($rows){
        return kernel::single('material_extracolumn_salesmaterial_unit')->process($rows);
    }

    /**
    * 销售物料的过滤方法
    */
    public function _filter($filter,$tableAlias=null,$baseWhere=null){
        if (isset($filter['not_gift']) && $filter['not_gift'] == 1){
            $where .= '  AND sales_material_type IN (1,2)';
            unset($filter['not_gift']);
        }
        
        #对应店铺
        if(isset($filter['in_shop_id']) && !empty($filter['in_shop_id']))
        {
            if(!is_array($filter['in_shop_id']))
            {
                $filter['in_shop_id']    = explode(',', $filter['in_shop_id']);
            }
            foreach ($filter['in_shop_id'] as $key => $val)
            {
                if(empty($val)) unset($filter['in_shop_id'][$key]);
            }
            
            $where    .= " AND shop_id IN ('". implode("','", $filter['in_shop_id']) ."') ";
            unset($filter['in_shop_id']);
        }
        
        return parent::_filter($filter,$tableAlias,$baseWhere).$where;
    }
}
